import user from '../../../utils/user'
import {
    getInsureData,
    substractInsurance,
    getInsureCompanyInfo,
} from '../../api/long-insure'
const app = getApp()
Page({
    data: {
        balance: 0,
        peopleNum: 0,
        planList: [],
        isExistAuto: Boolean,
        autoTerminalEnsureTime: '',
        isPopup: false,
        tcflag: true,
        longinsuretcflag: false,
        haslonginsure: false,
    },
    closelonginsuretc() {
        wx.setStorageSync('hascloselonginsuretc', 'true')
        this.setData({
            longinsuretcflag: false,
        })
    },
    async onLoad() {
        if (wx.getStorageSync('tcflag')) {
            this.setData({
                tcflag: true,
            })
        } else {
            this.setData({
                tcflag: false,
            })
        }
        await user.waitLogin()
        const currentTime = new Date()
        // 到2022-12-30号前都显示
        if (Number(currentTime) < 1672358400000) {
            this.setData({
                isPopup: true,
            })
        }
        if (!user.isOpenPeriodInsur) {
            console.log('请联系您的客服专员开通后再试')
            const eventChannel = this.getOpenerEventChannel()
            eventChannel.emit('callbackOperation', {
                data: 'longInsurance',
            })
            wx.navigateBack()
            return
        }
        const result = await getInsureData()
        // .map(item=>{
        //     item.isEnd = item.endTime ?  new Date(item.endTime).getTime() < new Date().getTime() : false
        //     return item;
        // })
        console.log(result)
        if (result.companyPlans) {
            if (result.companyPlans.length === 0) {
                this.setData({
                    haslonginsure: false,
                })
            } else {
                result.companyPlans.forEach((ele, i) => {
                    if (ele.insurCompany === 'period_003_yg') {
                        this.setData({
                            haslonginsure: true,
                        })
                        return
                    }
                })
            }
        } else {
            this.setData({
                haslonginsure: false,
            })
        }
        // result.companyPlans.forEach((ele,i)=>{
        //     if (ele.) {

        //     }
        // })
        this.setData({
            balance: result.accountBalance,
            peopleNum: result.allEnsureCount,
            planList: result.companyPlans,
        })
    },
    closetc() {
        this.setData({
            tcflag: false,
        })
        wx.setStorageSync('tcflag', false)
    },
    onShow() {
        if (wx.getStorageSync('hascloselonginsuretc')) {
            this.setData({
                longinsuretcflag: false,
            })
        } else {
            this.setData({
                longinsuretcflag: true,
            })
        }
        this.substractInsurance()
    },
    onRead() {
        this.setData({
            isPopup: false,
        })
    },
    onPlan(event) {
        wx.navigateTo({
            url:
                '/packageLongInsure/pages/detail/detail?id=' +
                event.currentTarget.dataset.id,
        })
    },
    onDispatch() {
        wx.navigateTo({
            url: '/packageLongInsure/pages/dispatch/dispatch',
        })
    },
    onPeople() {
        wx.navigateTo({
            // /packageLongInsure/pages/worker-list/worker-list?plant=1&type=checkbox 传递该参数则可以 单出现 多选
            url: '/packageLongInsure/pages/worker-list/worker-list',
        })
    },
    onReport() {
        wx.navigateTo({
            url: '/packageLongInsure/pages/report/report',
        })
    },
    async onFastInsure() {
        const res = await getInsureCompanyInfo({
            type: 3,
        })
        if (res.openQrProjectEnsure === 1) {
            wx.navigateTo({
                url: '/packageLongInsure/pages/fast-insure/fast-insure',
            })
        } else if (res.openQrProjectEnsure === 2) {
            wx.navigateTo({
                url: '/packageLongInsure/pages/not-opened-fast-page/not-opened-fast-page',
            })
        }
    },
    onRecharge() {
        // 携带 longTerm参数 该参数是 说明 从长期保跳转过来的
        wx.navigateTo({
            url:
                '/packageInsurance/pages/insurance-recharge/insurance-recharge?type=InsureRechargePeriodZgrs',
        })
    },
    substractInsurance() {
        substractInsurance().then((res) => {
            let isExistAuto =
                res.isExistAutoTerminalEnsure ||
                res.isExistAutoTerminalEnsureMonth
            this.setData({
                isExistAuto: isExistAuto,
            })
        })
    },
    onBill() {
        wx.navigateTo({
            url: '/packageLongInsure/pages/bill/bill',
        })
    },
    abatement() {
        wx.navigateTo({
            url: '/packageLongInsure/pages/reduce-insurance/reduce-insurance',
        })
    },
    // 去转出页面
    goTransferOut() {
        wx.navigateTo({
            url:
                '/packageInsurance/pages/balance-out/balance-out?type=longInsure',
        })
    },
})
